package com.aw.guiguzi.module.manager.transaction.presenters

import com.aw.baselib.base.AwCommonPresenter
import com.aw.baselib.bean.CommonImgItemBean
import com.aw.baselib.net.callback.AwApiCallback
import com.aw.baselib.util.AwFileUtil
import com.aw.baselib.util.AwLog
import com.aw.guiguzi.bean.UserInfoBean
import com.aw.guiguzi.module.manager.transaction.views.ManagerTransactionConfigView
import com.aw.guiguzi.net.observers.NetObCommon
import java.io.File

/**
 * @author : hzw
 */
class ManagerTransactionConfigPresent(private val mView: ManagerTransactionConfigView.View) :
    AwCommonPresenter(), ManagerTransactionConfigView.Presenter {
    override fun uploadFile(filePath: String?) {
        AwLog.d("Transaction upload path: $filePath")
        val file = File(filePath)
        if (!file.exists()) {
            AwLog.d("Transaction upload return file not exist")
            return
        }
        addIOSubscription<Any>(
            NetObCommon.uploadFile(filePath),
            object : AwApiCallback<Any>(mView, true, true) {
                override fun onSuccess(result: Any) {
                    AwLog.d("Transaction upload success: $result")
                    AwFileUtil.deleteFile(file.absoluteFile)
                    mView.uploadFileSuccess(result as String)
                }

                override fun onFail(code: Int, msg: String) {
                    super.onFail(code, msg)
                    AwLog.d("Transaction upload onFail code: $code ,msg: $msg")
                }
            })
    }

    override fun uploadFile(bean: CommonImgItemBean?, filePath: String?) {
        AwLog.d("Transaction upload path: $filePath")
        val file = File(filePath)
        if (!file.exists()) {
            AwLog.d("Transaction upload return file not exist")
            return
        }
        addIOSubscription<Any>(
            NetObCommon.uploadFile(filePath),
            object : AwApiCallback<Any>(mView, true, true) {
                override fun onSuccess(result: Any) {
                    AwLog.d("Transaction upload success: $result")
                    AwFileUtil.deleteFile(file.absoluteFile)
                    mView.uploadFileSuccess(bean, result as String)
                }

                override fun onFail(code: Int, msg: String) {
                    super.onFail(code, msg)
                    AwLog.d("Transaction upload onFail code: $code ,msg: $msg")
                }
            })
    }

    override fun updateUserRemark(bean: UserInfoBean?) {
        addIOSubscription(
            NetObCommon.updateUserInfo(bean),
            object : AwApiCallback<Boolean>(mView, true, true) {
                override fun onSuccess(result: Boolean) {
                    mView.updateUserRemarkSuccess()
                }

                override fun onFail(code: Int, msg: String) {
                    super.onFail(code, msg)
                    mView.showMsg(msg)
                }
            })
    }
}